/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import static GUI.MusicPlayerUI.play_pauseButtonClickedCount;
import engine.linkList.Init_Save;
import engine.linkList.PlayScheduleManager;
import engine.player.MusicPlayer;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Font;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author hjp
 */
//播放器的系统托盘菜单
public class MusicPlayerUI_Tray {
    private  JPopupMenu menu;//弹出式菜单
    private  JLabel musicPlayerLabel;
    public   TrayIcon trayIcon;//系统托盘图标的实例.为了能够动态的修改显示文本
    public   JMenuItem play_pause;//播放，暂停
    private  JMenuItem priorMusic;//上一首
    private  JMenuItem nextMusic;//下一首
    private  JMenuItem exit;//退出
    
    private Color color = new Color(250, 250, 250);
    private Font font = new Font("楷体",0, 24);
    
    private static MusicPlayerUI_Tray tray = new MusicPlayerUI_Tray();

    private MusicPlayerUI_Tray(){
        initTray();
    }
    
    public static MusicPlayerUI_Tray getInstance(){
        return tray;
    }
    
//    public void startTray(){
//        initTray();
//    }
    
    private void initTray(){
        /* 获取本操作系统的托盘的实例 */
        SystemTray tray = SystemTray.getSystemTray();
        /* 创建一个右键弹出式菜单 */
        menu = new JPopupMenu();

        //初始化label
        musicPlayerLabel = new JLabel();
        musicPlayerLabel.setIcon(new ImageIcon(getClass().getResource("/GUI/icons/icons8-披头士-48.png")));
        /* 初始化其他菜单项 */
        play_pause = new JMenuItem("播放");
        priorMusic = new JMenuItem("上一首");
        nextMusic = new JMenuItem("下一首");
        exit = new JMenuItem("退出");

        /* 按钮的数组 */
        JMenuItem[] items = {play_pause,priorMusic,nextMusic,exit};
        /* 对应每个按钮的图标的数组 */
        ImageIcon[] icons = {new ImageIcon(getClass().getResource("/GUI/icons/play.png")),
            new ImageIcon(getClass().getResource("/GUI/icons/pre.png")),
                new ImageIcon(getClass().getResource("/GUI/icons/next.png")),
               new ImageIcon(getClass().getResource("/GUI/icons/exit.png"))};
        /* 为所有AbstractButton设置UI和高亮 */
        for (int i = 0 ; i < items.length ; ++i){
            JMenuItem item = items[i];
            //item.setCursor(java.awt.Cursor.HAND_CURSOR);
            /* 为所有按钮设置UI以及相应的图标 */
            setUI(item,icons[i]);
            /* 为所有按钮添加鼠标经过的监听器，使得鼠标经过时按钮高亮 */
            setHighLight(item);
        }
        musicPlayerLabel.setBackground(color);
        musicPlayerLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                menu.setVisible(false);
            }
        });
        /* 菜单项的鼠标点击事件的监听器 */
        setMouseClickListener();
        /* 将组件添加进菜单里面 */

        menu.add(musicPlayerLabel);
        menu.add(play_pause);//播放或者暂停
        menu.add(priorMusic);//上一首
        menu.add(nextMusic);//下一首
        menu.add(exit);//退出

        //设置系统托盘图标
        ImageIcon icon = new ImageIcon(getClass().getResource("/GUI/icons/MusicPlayer.png"));
        trayIcon = new TrayIcon(icon.getImage());
        /* 托盘图标监听器 */
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1){
                    if (!MusicPlayerUI.getInstance().isVisible()){
                        MusicPlayerUI.getInstance().setVisible(true);
                     }
                }
                /* 当鼠标右键点击时,展开菜单栏 */
                if (e.getButton() == MouseEvent.BUTTON3){
                    /* 得到鼠标点击时的位置 */
                    Point point = MouseInfo.getPointerInfo().getLocation();
                    menu.setLocation(point);
                    menu.setVisible(true);
                }
            }
        });

        /* 将CloudMusic添加到系统托盘 */
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
    
    private void setUI(JMenuItem item,ImageIcon icon){
        /* 设定按钮图标 */
        item.setIcon(icon);
        //item.setCursor(Cursor.HAND);
        /* 设定按钮背景 */
        item.setBackground(color);
        /* 设定按钮字体 */
        item.setFont(font);
        /* 设定按钮边框 */
        item.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5));
    }
    
    private void setHighLight(JMenuItem item){
        item.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                item.setBackground(Color.GREEN);
            }
            /* 鼠标离开时恢复原状态 */
            @Override
            public void mouseExited(MouseEvent e) {
                item.setBackground(color);
            }
        });
    }
    
    private void setMouseClickListener(){
        /* 播放或暂停按钮 */
        play_pause.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1 && PlayScheduleManager.getInstance().currentPlayMusicList != null) {
                    //UI操作
                    play_pauseButtonClickedCount++;
                    play_pauseButtonClickedCount = play_pauseButtonClickedCount % 2;
                    Icon icon = null;
                    switch(play_pauseButtonClickedCount){
                        case 0:{
                             icon = new ImageIcon(getClass().getResource("/GUI/icons/play.png"));
                             play_pause.setText("播放");
                             //执行暂停播放
                             MusicPlayer.musicPlayer.pausePlay();
                            break;
                        }
                        case 1:{
                            play_pause.setText("暂停");
                             icon = new ImageIcon(getClass().getResource("/GUI/icons/pause.png"));
                             //执行继续播放
                             MusicPlayer.musicPlayer.continuePlay();
                            break;
                        }
                    }
                    play_pause.setIcon(icon);
                    MusicPlayerUI.getInstance().getPlay_PauseButton().setIcon(icon);
                    menu.setVisible(false);
                }
            }
        });
        priorMusic.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1 && PlayScheduleManager.getInstance().currentPlayMusicList != null){
                     MusicPlayer.musicPlayer.playMp3FileImmediately(PlayScheduleManager.getInstance().getPrePlayDefault().info.getFilePath());
                    //更新musicNameLabel
                    MusicPlayerUI.getInstance().getMusicNameLabel().setText(PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getSongName());
                    //设置currentPlayTimeJSlider以及currentPlayTimeJLabel
                    MusicPlayerUI.getInstance().getCurrentPlayTimeJSlider().setMaximum(PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getTime_Int());
                    MusicPlayerUI.getInstance().getCurrentPlayTimeJSlider().setValue(0);
                    MusicPlayerUI.getInstance().getCurrentPlayTimeJLabel().setText("00:00" +  "/" + PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getTime_String());
                     //重置播放进度条线程的currentPlayTime
                    MusicPlayer.currentPlayTime = 0;
                    menu.setVisible(false);
                }
            }
        });
        nextMusic.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1 && PlayScheduleManager.getInstance().currentPlayMusicList != null){
                     MusicPlayer.musicPlayer.playMp3FileImmediately(PlayScheduleManager.getInstance().getNextPlayDefault().info.getFilePath());
                    //更新musicNameLabel
                    MusicPlayerUI.getInstance().getMusicNameLabel().setText(PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getSongName());
                    //设置currentPlayTimeJSlider以及currentPlayTimeJLabel
                    MusicPlayerUI.getInstance().getCurrentPlayTimeJSlider().setMaximum(PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getTime_Int());
                    MusicPlayerUI.getInstance().getCurrentPlayTimeJSlider().setValue(0);
                    MusicPlayerUI.getInstance().getCurrentPlayTimeJLabel().setText("00:00" +  "/" + PlayScheduleManager.getInstance().pCurrentPlayMusicNodeCopy.info.getTime_String());
                    //重置播放进度条线程的currentPlayTime
                    MusicPlayer.currentPlayTime = 0;
                    menu.setVisible(false);
                }
            }
        });
        exit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Init_Save.save();
                System.exit(0);
            }
        });
    }
}
